﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XMLGener.Interfaces;

namespace XBasicModules
{
    /// <summary>
    /// 
    /// </summary>
    public class ShowOutput : ModuleBase, XMLGenerOutputInterface
    {
        private static string definition = "<function>" +
             "    <name>Execute application</name>" +
             "    <id>executeapplication</id>" +
             "    <type>process</type>" +
             "    <attributes>" +
             "       <attribute>" +
             "          <name>Application</name>" +
             "          <id>application</id>" +
             "          <type>file</type>" +
             "       </attribute>" +
             "       <attribute>" +
             "          <name>Arguments</name>" +
             "          <id>arguments</id>" +
             "          <type>text</type>" +
             "       </attribute>" +
             "       <attribute>" +
             "          <name>Text ${FILE} in arguments will be replaced by generated document.</name>" +
             "          <id>info</id>" +
             "          <type>info</type>" +
             "       </attribute>" +
             "    </attributes>" +
             "</function>";

       
        string arguments = null;
        string application = null;

        #region XMLGenerOutputInterface Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="outputFile"></param>
        public void generateOutput(string inputFile, string outputFile)
        {
            processOutput(inputFile);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        public void processOutput(string fileName)
        {
            string exeArguments = arguments.Replace("${FILE}", "\""+fileName)+"\"";
            string appl = application;
            if (string.IsNullOrEmpty(appl))
            {
                appl = "explorer.exe";
                exeArguments = "/select," + fileName;
            }
            try
            {
                System.Diagnostics.Process.Start(appl, exeArguments);
            }
            catch (Exception ex)
            {
                logger.addErrorLog(this.GetType().ToString(), ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string getDefinition()
        {
            return definition;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlParams"></param>
        public void setParameters(string xmlParams)
        {
            if (!this.checkSettingsElement(xmlParams, null))
                return;

            this.arguments = this.getParam("arguments");
            this.application = this.getParam("application");

        }

        #endregion

    }
}
